using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace WowAceUpdater
{
    internal class TextBoxWriter : TextWriter
    {
        #region Fields

        private readonly TextBoxBase _textBox;

        #endregion

        #region Nested type: AppendFunction

        private delegate void AppendFunction(Color color, string p);

        #endregion

        #region Nested type: ClearFunction

        private delegate void ClearFunction();

        #endregion

        #region Nested type: ScrollToCaretFunction

        private delegate void ScrollToCaretFunction();

        #region Constructors

        public TextBoxWriter(TextBoxBase textBox)
        {
            _textBox = textBox;
        }

        #endregion

        #region Properties

        public override Encoding Encoding
        {
            get { return Encoding.Default; }
        }

        #endregion

        #region Public Methods

        public void Clear()
        {
            if (_textBox.IsDisposed)
                return;

            if (_textBox.InvokeRequired)
                _textBox.BeginInvoke(new ClearFunction(Clear));
            else
                _textBox.Clear();
        }

        public void ScrollToCaret()
        {
            if (_textBox.IsDisposed)
                return;

            if (_textBox.InvokeRequired)
                _textBox.BeginInvoke(new ScrollToCaretFunction(ScrollToCaret));
            else
                _textBox.ScrollToCaret();
        }

        public void WriteColored(Color color, string value)
        {
            if (_textBox.IsDisposed)
                return;

            if (_textBox.InvokeRequired)
                _textBox.BeginInvoke(new AppendFunction(WriteColored), new object[] {color, value});
            else
            {
                Program.LogDbg(value);

                if (_textBox is RichTextBox)
                {


                    (_textBox as RichTextBox).SelectionColor = color;
                    _textBox.AppendText(value);
                    (_textBox as RichTextBox).SelectionColor = _textBox.ForeColor;
                }
                else
                    _textBox.AppendText(value);
            }
        }

        // The base class implementations (string, object) and (string, object, object)
        // call this using string.format(value, object, object)
        public override void Write(string value)
        {
            if (_textBox.IsDisposed)
                return;

            

            if (_textBox.InvokeRequired)
                _textBox.BeginInvoke(new AppendFunction(WriteColored), new object[] {_textBox.ForeColor, value});
            else
                WriteColored(_textBox.ForeColor, value);
        }

        public void WriteLineColored(Color color, string value)
        {
            WriteColored(color, value + "\r\n");
            ScrollToCaret();
        }

        // The base class implementations (string, object) and (string, object, object)
        // call this using string.format(value, object, object)
        public override void WriteLine(string value)
        {
            Write(value + "\r\n");
            ScrollToCaret();
        }

        #endregion

        #endregion
    }
}